package com.milepost.workflow.flowinsttask.ctrl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.milepost.workflow.common.dto.BootstrapTableVoDto;
import com.milepost.workflow.common.enums.DsKeyEnum;
import com.milepost.workflow.common.vo.BootstrapTableVo;
import com.milepost.workflow.config.dynamicDs.DataSourceContextHolder;
import com.milepost.workflow.flowinsttask.dto.ApproveTaskDto;
import com.milepost.workflow.flowinsttask.dto.ApprovedTaskDto;
import com.milepost.workflow.flowinsttask.service.FlowInstTaskService;
import com.milepost.workflow.flowinsttask.vo.ApproveTaskVo;
import com.milepost.workflow.flowinsttask.vo.ApprovedTaskVo;
import com.milepost.workflow.flowinsttaskapp.entity.FlowInstTaskAppEntity;
import com.milepost.workflow.response.Response;
import com.milepost.workflow.response.ResponseHelper;
import com.milepost.workflow.response.ReturnCode;
import com.milepost.workflow.user.entity.UsersEntity;
import com.milepost.workflow.user.service.UserService;
import com.milepost.workflow.util.SessionUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author huarf
 * @Description
 * @create 2022-03-28 23:47
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/flowInstTask")
public class FlowInstTaskCtrl {

    private final FlowInstTaskService flowInstTaskService;

    private final UserService userService;

    /**
     * 办理任务
     * @param flowInstTaskAppEntity
     * @return
     */
    @PostMapping("/approveTask")
    public Response<FlowInstTaskAppEntity> approveTask(@RequestBody FlowInstTaskAppEntity flowInstTaskAppEntity, HttpSession session) {
        Response<FlowInstTaskAppEntity> response = null;

        try {
            flowInstTaskAppEntity.setAssignee(SessionUtil.getCurrUser(session).getId());
            Boolean success = flowInstTaskService.approveTask(flowInstTaskAppEntity);
            if (success) {
                response = ResponseHelper.createResponse(ReturnCode.SUCCESS, "审批成功");
            } else {
                response = ResponseHelper.createResponse(ReturnCode.FAIL, "审批失败");
            }
            response.setPayload(flowInstTaskAppEntity);
        } catch (Exception e) {
            log.error("审批任务异常", e);
            response = ResponseHelper.createExceptionResponse(e);
        }

        return response;
    }

    /**
     * 查询待办任务列表
     *
     * @param bootstrapTableVoDto
     * @param approveTaskDto
     * @return
     */
    @GetMapping("/getApproveTaskList")
    public Response<BootstrapTableVo<ApproveTaskVo>> getApproveTaskList(BootstrapTableVoDto bootstrapTableVoDto, ApproveTaskDto approveTaskDto, HttpSession session) {

        Response<BootstrapTableVo<ApproveTaskVo>> response = null;

        try {
            approveTaskDto.setUserId(SessionUtil.getCurrUser(session).getId());
            Page<ApproveTaskVo> approveTaskVoPage = flowInstTaskService.getApproveTaskList(bootstrapTableVoDto, approveTaskDto);

            // 填充用户名称
            DataSourceContextHolder.setDataSource(DsKeyEnum.USER.getKey());
            for (ApproveTaskVo record : approveTaskVoPage.getRecords()) {
                if (StringUtils.isNoneBlank(record.getFlowStartUser())) {
                    UsersEntity userEntity = userService.getById(record.getFlowStartUser());
                    if (userEntity != null) {
                        record.setFlowStartUserName(userEntity.getName());
                    }
                }
            }

            BootstrapTableVo<ApproveTaskVo> bootstrapTableVo = new BootstrapTableVo<>(approveTaskVoPage);
            response = ResponseHelper.createSuccessResponse(bootstrapTableVo);
        } catch (Exception e) {
            log.error("查询待办任务列表异常", e);
            response = ResponseHelper.createExceptionResponse(e);
        } finally {
            DataSourceContextHolder.clearDataSource();
        }

        return response;
    }

    /**
     * 查询已办任务列表
     *
     * @param bootstrapTableVoDto
     * @param approvedTaskDto
     * @return
     */
    @GetMapping("/getApprovedTaskList")
    public Response<BootstrapTableVo<ApprovedTaskVo>> getAprovedTaskList(BootstrapTableVoDto bootstrapTableVoDto, ApprovedTaskDto approvedTaskDto, HttpSession session) {

        Response<BootstrapTableVo<ApprovedTaskVo>> response = null;

        try {
            approvedTaskDto.setUserId(SessionUtil.getCurrUser(session).getId());
            Page<ApprovedTaskVo> approvedTaskVoPage = flowInstTaskService.getApprovedTaskList(bootstrapTableVoDto, approvedTaskDto);

            // 填充用户名称
            DataSourceContextHolder.setDataSource(DsKeyEnum.USER.getKey());
            for (ApprovedTaskVo record : approvedTaskVoPage.getRecords()) {
                if (StringUtils.isNoneBlank(record.getFlowStartUser())) {
                    UsersEntity userEntity = userService.getById(record.getFlowStartUser());
                    if (userEntity != null) {
                        record.setFlowStartUserName(userEntity.getName());
                    }
                }
            }

            BootstrapTableVo<ApprovedTaskVo> bootstrapTableVo = new BootstrapTableVo<>(approvedTaskVoPage);
            response = ResponseHelper.createSuccessResponse(bootstrapTableVo);
        } catch (Exception e) {
            log.error("查询已办任务列表异常", e);
            response = ResponseHelper.createExceptionResponse(e);
        } finally {
            DataSourceContextHolder.clearDataSource();
        }

        return response;
    }

}
